package com.hyc.myvideo.ui.activity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v4.app.Fragment;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.hyc.myvideo.R;
import com.hyc.myvideo.entity.TabEntity;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.myvideo.ui.fragment.homefargment.ColumnFragment;
import com.hyc.myvideo.ui.fragment.homefargment.HomeFragment;
import com.hyc.myvideo.ui.fragment.homefargment.LiveFragment;
import com.hyc.myvideo.ui.fragment.homefargment.MyFragment;
import com.hyc.myvideo.ui.service.AudioService;

import java.util.ArrayList;

import butterknife.BindView;
import timber.log.Timber;

/**
 * 首页容器
 * @author gyb
 * @version 1.0
 * @created 2017/06/16
 */
public class MainActivity extends BaseFActivity {
    @BindView(R.id.home_tab_icon)
    public CommonTabLayout tabLayout;
    private String[] mTitles = {"首页", "直播", "专栏", "我的"};
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private int[] mIconUnselectIds = {
            R.mipmap.home_icon_home, R.mipmap.home_icon_live,
            R.mipmap.home_icon_column, R.mipmap.home_icon_my};

    private int[] mIconSelectIds = {
            R.mipmap.home_icon_home_select, R.mipmap.home_icon_live_select,
            R.mipmap.home_icon_column_select, R.mipmap.home_icon_my_select};
    private ArrayList<CustomTabEntity> tabs = new ArrayList<>();

    private ServiceConnection connection;
    @Override
    public int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        if (audioService==null){
            connection=new Connection();
            startService(new Intent(this, AudioService.class));
            bindService(new Intent(this, AudioService.class), connection, Service.BIND_AUTO_CREATE);
        }
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new LiveFragment());
        fragments.add(new ColumnFragment());
        fragments.add(new MyFragment());
        for (int i = 0; i < mTitles.length; i++) {
            tabs.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        tabLayout.setTabData(tabs,this,R.id.home_tab_frame,fragments);
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
            }
            @Override
            public void onTabReselect(int position) {
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void oNext(String resulte, String method) {

    }

    @Override
    protected void oError() {

    }

    private class Connection implements ServiceConnection{

        @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            AudioService.AudioBinder audioBinder = (AudioService.AudioBinder) iBinder;
            AudioService audioService=audioBinder.getService();
            appContext.setAudioService(audioService);
        }

        @Override public void onServiceDisconnected(ComponentName componentName) {

        }
    }


    @Override protected void onDestroy() {
        if (connection!=null){
            unbindService(connection);
        }
        Timber.e("==onDestroy=connection!=null="+(connection!=null));
        super.onDestroy();
    }
}
